package auth

import (
	"github.com/gin-gonic/gin"
	logic2 "iot-base/auth/api/logic"
	"iot-base/auth/logger"
	types2 "iot-base/auth/types"
)

type ResUserRole struct {
	RoleIds []int64 `json:"role_ids"`
}

type UsesRole struct {
	Uid     int64   `json:"uid"`
	RoleIds []int64 `json:"role_ids"`
}
type ResDelUserRole struct {
	RoleIds  bool `json:"role_ids"`
	GroupIds bool `json:"group_ids"`
}

// EditUserRole 编辑用户角色.
func EditUserRole(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	resp := types2.NewResult(c)
	var userRole UsesRole
	err := c.ShouldBind(&userRole)
	if err != nil {
		resp.ErrorCode(types2.ParamsError, err.Error())
		return
	}
	logic := logic2.NewUserRoleLogic()
	err = logic.EditUserRole(userRole.Uid, userRole.RoleIds)
	if err != nil {
		logger.Log.Errorf("[%v] err:=[%v]", requestId, err)
		resp.ErrorCode(types2.ExceptionError, err.Error())
	}
	resp.Success(gin.H{})
}

// FindUserRole 获取用户角色.
func FindUserRole(c *gin.Context) {
	result := types2.NewResult(c)
	//requestId, _ := c.Get("RequestId")
	var req types2.ReqGetUserRole
	err := c.ShouldBind(&req)
	if err != nil {
		result.ErrorCode(types2.ParamsError, "")
		return
	}
	roleLogic := logic2.NewUserRoleLogic()
	resp, err := roleLogic.FindUserRole(req.Uid)
	if err != nil {
		result.ErrorCode(types2.DBError, err.Error())
	}
	result.Success(resp)
}
